package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 页面跳转
 */
@Controller
public class PageJump {
    @RequestMapping("/modelAndView01")
    public ModelAndView response_ModelAndView01() {
        /**
         * model：模型 封装数据
         * view:视图 展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据(键值对)
        modelAndView.addObject("msg","anonyeast");
        //设置视图名称
        modelAndView.setViewName("modelAndView");//拼接后:/jsp/modelAndView.jsp
        return modelAndView;
    }

    /**
     * 直接在参数列表注入ModelAndView
     * @param modelAndView
     * @return modelAndView
     */
    @RequestMapping("/modelAndView02")
    public ModelAndView response_ModelAndView02(ModelAndView modelAndView) {
        //设置模型数据(键值对)
        modelAndView.addObject("msg","XHH");
        //设置视图名称
        modelAndView.setViewName("modelAndView");//拼接后:/jsp/modelAndView.jsp
        return modelAndView;
    }

    /**
     * 将Model和View分离
     * @param model
     * @return 视图
     */
    @RequestMapping("/modelAndView03")
    public String response_ModelAndView03(Model model) {
        //设置模型数据(键值对)
        model.addAttribute("msg","YDD");
        return "modelAndView";//拼接后:/jsp/modelAndView.jsp
    }

    /**
     * 向request域存储数据(不常用)
     * @param request Request请求
     * @return 视图
     */
    @RequestMapping("/modelAndView04")
    public String response_ModelAndView04(HttpServletRequest request) {
        //设置模型数据(键值对)
        request.setAttribute("msg","You added this message to Request");
        return "modelAndView";//拼接后:/jsp/modelAndView.jsp
    }
}

